package com.lineage.server.serverpackets;

import com.lineage.server.datatables.ShopCnTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.templates.L1ShopItem;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class S_ShopBuyListCn extends ServerBasePacket {
  private byte[] _byte = null;
  
  public S_ShopBuyListCn(L1PcInstance pc, L1NpcInstance npc) {
    writeC(65);
    writeD(npc.getId());
    ArrayList<L1ShopItem> shopItems = ShopCnTable.get().get(
        npc.getNpcId());
    if (shopItems.size() <= 0) {
      pc.sendPackets(new S_NoSell(npc));
      return;
    } 
    Map<Integer, Integer> assessedItems = new HashMap<>();
    for (L1ItemInstance item : pc.getInventory().getItems()) {
      switch (item.getItem().getItemId()) {
        case 40308:
        case 40314:
        case 40316:
        case 44070:
          continue;
      } 
      if (item.isEquipped())
        continue; 
      if (item.getBless() >= 128)
        continue; 
      if (item.get_time() != null)
        continue; 
      for (L1ShopItem shopItem : shopItems) {
        if (shopItem.getItemId() != item.getItemId() || 
          shopItem.getPurchasingPrice() < 0)
          continue; 
        pc.get_otherList().add_cnList(shopItem, item.getId());
        assessedItems.put(Integer.valueOf(item.getId()), 
            Integer.valueOf(shopItem.getPurchasingPrice()));
      } 
    } 
    if (assessedItems.size() <= 0) {
      pc.sendPackets(new S_NoSell(npc));
      return;
    } 
    writeH(assessedItems.size());
    for (Map.Entry<Integer, Integer> entrySet : assessedItems.entrySet()) {
      writeD(((Integer)entrySet.getKey()).intValue());
      writeD(((Integer)entrySet.getValue()).intValue());
    } 
  }
  
  public byte[] getContent() {
    if (this._byte == null)
      this._byte = getBytes(); 
    return this._byte;
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
